package com.syh.jn.scm.service.impl.supplier.statistics;

import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.jn.scm.domain.supplier.entity.base.goods.Goods;
import com.syh.jn.scm.domain.supplier.entity.base.goods.GoodsBrand;
import com.syh.jn.scm.domain.supplier.entity.base.goods.GoodsCategory;
import com.syh.jn.scm.domain.supplier.repository.base.goods.GoodsBrandReadRepository;
import com.syh.jn.scm.domain.supplier.repository.base.goods.GoodsCategoryReadRepository;
import com.syh.jn.scm.domain.supplier.repository.base.goods.GoodsReadRepository;
import com.syh.jn.scm.dto.supplier.statistics.purchase.GoodsBrandReportVo;
import com.syh.jn.scm.dto.supplier.statistics.purchase.GoodsCateReportVo;
import com.syh.jn.scm.dto.supplier.statistics.purchase.GoodsPurchaseReportVo;
import com.syh.jn.scm.dto.supplier.statistics.purchase.UpstreamSupplierReportVo;
import com.syh.jn.scm.dto.supplier.statistics.stock.InventoryReportVo;
import com.syh.jn.scm.infrastructure.repository.utils.MybatisTemplate;
import com.syh.jn.scm.service.api.supplier.statistics.PurchaseReportService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购报表相关
 */
@Service
public class PurchaseReportServiceImpl implements PurchaseReportService {

  /**
   * 根据商品进行统计采购
   *
   * @param params
   * @return
   */
  @Override
  public List<GoodsPurchaseReportVo> goodsPurchaseReport(Map<String, Object> params) {
//    return MybatisTemplate.selectList("purchaseReportQRepository.goodsPurchaseReport",params);
    return this.getGoodsPurchaseReport(params);
  }

  private List<GoodsPurchaseReportVo> getGoodsPurchaseReport(Map<String, Object> params) {
    GoodsReadRepository goodsReadRepository = (GoodsReadRepository) SpringContextUtils.getBean("goodsRepositoryImpl");
    //获取要分页统计的商品列表
    Integer startRow = (Integer) params.get("startRow");
    Integer pageSize = (Integer) params.get("pageSize");
    Long businessEntityId = (Long) params.get("businessEntityId");
    //报表统计开始时间 如果没有选择开始时间  默认是从最初开始查询
    String reportBeginTimeStamp = params.get("beginTime") == null ? "0" : (String)params.get("beginTime");
    //如果没有选择结束时间  默认到当前时间
    String reportEndTimeStamp = params.get("endTime") == null ? String.valueOf(System.currentTimeMillis()) : (String)params.get("endTime");
    Wrapper goodsWp = Wrapper.build();
    this.setQuery(goodsWp,params);
    List<Goods> goodsList = goodsReadRepository.findListPage(goodsWp,startRow,pageSize);
    Map<String, InventoryReportVo> inventoryReportVoMapTemp = StockInventoryUtils.getStockInventory(reportBeginTimeStamp,reportEndTimeStamp,businessEntityId);
    Map<Long,InventoryReportVo> inventoryReportVoMap = new HashMap<>();
    inventoryReportVoMapTemp.forEach((key,inventoryReportVo) -> {
      if (inventoryReportVoMap.get(inventoryReportVo.getGoodsId()) != null) {
        InventoryReportVo inventoryReportVo1 = inventoryReportVoMap.get(inventoryReportVo.getGoodsId());
        inventoryReportVo1.setPurQuantity(inventoryReportVo1.getPurQuantity().add(inventoryReportVo.getPurQuantity()));
        inventoryReportVo1.setPurAmount(inventoryReportVo1.getPurAmount().add(inventoryReportVo.getPurAmount()));
        inventoryReportVo1.setPurRetQuantity(inventoryReportVo1.getPurRetQuantity().add(inventoryReportVo.getPurRetQuantity()));
        inventoryReportVo1.setPurRetAmount(inventoryReportVo1.getPurRetAmount().add(inventoryReportVo.getPurRetAmount()));
        inventoryReportVo1.setPurLossQuantity(inventoryReportVo1.getPurLossQuantity().add(inventoryReportVo.getPurLossQuantity()));
        inventoryReportVo1.setPurLossAmount(inventoryReportVo1.getPurLossAmount().add(inventoryReportVo.getPurLossAmount()));
        inventoryReportVo1.setPurRetLossQuantity(inventoryReportVo1.getPurRetLossQuantity().add(inventoryReportVo.getPurRetLossQuantity()));
        inventoryReportVo1.setPurRetLossAmount(inventoryReportVo1.getPurRetLossAmount().add(inventoryReportVo.getPurRetLossAmount()));
        inventoryReportVo1.setPurSpillQuantity(inventoryReportVo1.getPurSpillQuantity().add(inventoryReportVo.getPurSpillQuantity()));
        inventoryReportVo1.setPurSpillAmount(inventoryReportVo1.getPurSpillAmount().add(inventoryReportVo.getPurSpillAmount()));
        inventoryReportVo1.setPurRetSpillQuantity(inventoryReportVo1.getPurRetSpillQuantity().add(inventoryReportVo.getPurRetSpillQuantity()));
        inventoryReportVo1.setPurRetSpillAmount(inventoryReportVo1.getPurRetSpillAmount().add(inventoryReportVo.getPurRetSpillAmount()));
      } else {
        inventoryReportVoMap.put(inventoryReportVo.getGoodsId(),inventoryReportVo);
      }
    });
    List<GoodsPurchaseReportVo> goodsPurchaseReportVos = new ArrayList<>();
    goodsList.forEach(goods -> {
      if (inventoryReportVoMap.get(goods.getId()) != null) {
        GoodsPurchaseReportVo goodsPurchaseReportVo = new GoodsPurchaseReportVo();
        InventoryReportVo inventoryReportVo = inventoryReportVoMap.get(goods.getId());
        goodsPurchaseReportVo.setGoodsId(goods.getId());
        goodsPurchaseReportVo.setGoodsCode(goods.getCode());
        goodsPurchaseReportVo.setGoodsName(goods.getName());
        goodsPurchaseReportVo.setGoodsSpecName(goods.getSpecName());
        goodsPurchaseReportVo.setGoodsUnits(goods.getUnits());
        goodsPurchaseReportVo.setPurQuantity(inventoryReportVo.getPurQuantity());
        goodsPurchaseReportVo.setPurAmt(inventoryReportVo.getPurAmount());
        goodsPurchaseReportVo.setPurRetQuantity(inventoryReportVo.getPurRetQuantity());
        goodsPurchaseReportVo.setPurRetAmt(inventoryReportVo.getPurRetAmount());
        goodsPurchaseReportVo.setLossQuantity(inventoryReportVo.getPurLossQuantity().add(inventoryReportVo.getPurRetLossQuantity()));
        goodsPurchaseReportVo.setLossAmt(inventoryReportVo.getPurLossAmount().add(inventoryReportVo.getPurRetLossAmount()));
        goodsPurchaseReportVo.setSpillQuantity(inventoryReportVo.getPurSpillQuantity().add(inventoryReportVo.getPurSpillQuantity()));
        goodsPurchaseReportVo.setSpillAmt(inventoryReportVo.getPurSpillAmount().add(inventoryReportVo.getPurSpillAmount()));

        //符号纠正
        goodsPurchaseReportVo.setPurRetQuantity(goodsPurchaseReportVo.getPurRetQuantity().negate());
        goodsPurchaseReportVo.setPurRetAmt(goodsPurchaseReportVo.getPurRetAmt().negate());
        goodsPurchaseReportVo.setLossQuantity(goodsPurchaseReportVo.getLossQuantity().negate());
        goodsPurchaseReportVo.setLossAmt(goodsPurchaseReportVo.getLossAmt().negate());
        goodsPurchaseReportVos.add(goodsPurchaseReportVo);
      }
    });
    return goodsPurchaseReportVos;
  }

  /**
   * 根据商品进行统计采购总条数
   *
   * @param params
   * @return
   */
  @Override
  public Integer goodsPurchaseReportCount(Map<String, Object> params) {
//    return MybatisTemplate.selectOne("purchaseReportQRepository.goodsPurchaseReportCount",params);
    GoodsReadRepository goodsReadRepository = (GoodsReadRepository) SpringContextUtils.getBean("goodsRepositoryImpl");
    Wrapper goodsWp = Wrapper.build();
    this.setQuery(goodsWp,params);
    return goodsReadRepository.getCount(goodsWp);
  }

  private void setQuery(Wrapper wrapper, Map<String,Object> params) {
    wrapper.append(Restrictions.eq("businessEntityId",(Long)params.get("businessEntityId")));
    if (params.get("goodsName") != null) {
      wrapper.append(Restrictions.like("name",(String)params.get("goodsName")));
    }
    if (params.get("goodsId") != null) {
      wrapper.append(Restrictions.eq("id",(String)params.get("goodsId")));
    }
    if (params.get("goodsCategoryId") != null) {
      wrapper.append(Restrictions.eq("id",(String)params.get("goodsCategoryId")));
    }
    if (params.get("goodsBrandId") != null) {
      wrapper.append(Restrictions.eq("id",(String)params.get("goodsBrandId")));
    }
  }

  /**
   * 根据供应商统计采购
   *
   * @param params
   * @return
   */
  @Override
  public List<UpstreamSupplierReportVo> upstreamSupplierReport(Map<String, Object> params) {
    return MybatisTemplate.selectList("purchaseReportQRepository.upstreamSupplierReport",params);
  }

  /**
   * 根据供应商统计总条数
   *
   * @param params
   * @return
   */
  @Override
  public Integer upstreamSupplierReportCount(Map<String, Object> params) {
    return MybatisTemplate.selectOne("purchaseReportQRepository.upstreamSupplierReportCount",params);
  }

  /**
   * 根据商品分类统计采购
   *
   * @param params
   * @return
   */
  @Override
  public List<GoodsCateReportVo> goodsCateReport(Map<String, Object> params) {
//    return MybatisTemplate.selectList("purchaseReportQRepository.goodsCateReport",params);
    return this.getGoodsCateReport(params);
  }

  private List<GoodsCateReportVo> getGoodsCateReport(Map<String, Object> params) {
    GoodsCategoryReadRepository goodsCategoryReadRepository = (GoodsCategoryReadRepository) SpringContextUtils.getBean("goodsCategoryRepositoryImpl");
    //获取要分页统计的商品列表
    Integer startRow = (Integer) params.get("startRow");
    Integer pageSize = (Integer) params.get("pageSize");
    Long businessEntityId = (Long) params.get("businessEntityId");
    //报表统计开始时间 如果没有选择开始时间  默认是从最初开始查询
    String reportBeginTimeStamp = params.get("beginTime") == null ? "0" : (String)params.get("beginTime");
    //如果没有选择结束时间  默认到当前时间
    String reportEndTimeStamp = params.get("endTime") == null ? String.valueOf(System.currentTimeMillis()) : (String)params.get("endTime");
    Wrapper goodsCategoryWp = Wrapper.build();
    this.setQuery(goodsCategoryWp,params);
    List<GoodsCategory> goodsCategoryList = goodsCategoryReadRepository.findListPage(goodsCategoryWp,startRow,pageSize);
    Map<String,InventoryReportVo> inventoryReportVoMapTemp = StockInventoryUtils.getStockInventory(reportBeginTimeStamp,reportEndTimeStamp,businessEntityId);
    Map<Long,InventoryReportVo> inventoryReportVoMap = new HashMap<>();
    //把获取到的InventoryReportVo数据按商品分类重新汇总成inventoryReportVoMap
    inventoryReportVoMapTemp.forEach((key,inventoryReportVo) -> {
      if (inventoryReportVoMap.get(inventoryReportVo.getGoodsCategoryId()) != null) {
        InventoryReportVo inventoryReportVo1 = inventoryReportVoMap.get(inventoryReportVo.getGoodsCategoryId());
        inventoryReportVo1.setPurQuantity(inventoryReportVo1.getPurQuantity().add(inventoryReportVo.getPurQuantity()));
        inventoryReportVo1.setPurAmount(inventoryReportVo1.getPurAmount().add(inventoryReportVo.getPurAmount()));
        inventoryReportVo1.setPurRetQuantity(inventoryReportVo1.getPurRetQuantity().add(inventoryReportVo.getPurRetQuantity()));
        inventoryReportVo1.setPurRetAmount(inventoryReportVo1.getPurRetAmount().add(inventoryReportVo.getPurRetAmount()));
        inventoryReportVo1.setPurLossQuantity(inventoryReportVo1.getPurLossQuantity().add(inventoryReportVo.getPurLossQuantity()));
        inventoryReportVo1.setPurLossAmount(inventoryReportVo1.getPurLossAmount().add(inventoryReportVo.getPurLossAmount()));
        inventoryReportVo1.setPurRetLossQuantity(inventoryReportVo1.getPurRetLossQuantity().add(inventoryReportVo.getPurRetLossQuantity()));
        inventoryReportVo1.setPurRetLossAmount(inventoryReportVo1.getPurRetLossAmount().add(inventoryReportVo.getPurRetLossAmount()));
        inventoryReportVo1.setPurSpillQuantity(inventoryReportVo1.getPurSpillQuantity().add(inventoryReportVo.getPurSpillQuantity()));
        inventoryReportVo1.setPurSpillAmount(inventoryReportVo1.getPurSpillAmount().add(inventoryReportVo.getPurSpillAmount()));
        inventoryReportVo1.setPurRetSpillQuantity(inventoryReportVo1.getPurRetSpillQuantity().add(inventoryReportVo.getPurRetSpillQuantity()));
        inventoryReportVo1.setPurRetSpillAmount(inventoryReportVo1.getPurRetSpillAmount().add(inventoryReportVo.getPurRetSpillAmount()));
      } else {
        inventoryReportVoMap.put(inventoryReportVo.getGoodsCategoryId(),inventoryReportVo);
      }
    });
    //用于存放计算时GoodsCateReportVo
    Map<Long,GoodsCateReportVo> goodsCateReportVoMap = new HashMap<>();
    //用于存放最后输入的GoodsCateReportVo
    List<GoodsCateReportVo> goodsCateReportVos = new ArrayList<>();
    goodsCategoryList.forEach(goodsCategory -> {
      GoodsCateReportVo goodsCateReportVo = new GoodsCateReportVo();
      goodsCateReportVo.setGoodsCategoryId(goodsCategory.getId());
      goodsCateReportVo.setGoodsCategoryCode(goodsCategory.getCode());
      goodsCateReportVo.setGoodsCategoryName(goodsCategory.getName());
      goodsCateReportVo.setGoodsCateLevel(goodsCategory.getCateLevel());
      goodsCateReportVo.setGoodsSupCategoryId(goodsCategory.getSupId());
      goodsCateReportVo.setPurQuantity(BigDecimal.ZERO);
      goodsCateReportVo.setPurAmt(BigDecimal.ZERO);
      goodsCateReportVo.setPurRetQuantity(BigDecimal.ZERO);
      goodsCateReportVo.setPurRetAmt(BigDecimal.ZERO);
      goodsCateReportVo.setLossQuantity(BigDecimal.ZERO);
      goodsCateReportVo.setLossAmt(BigDecimal.ZERO);
      goodsCateReportVo.setSpillQuantity(BigDecimal.ZERO);
      goodsCateReportVo.setSpillAmt(BigDecimal.ZERO);
      goodsCateReportVoMap.put(goodsCategory.getId(),goodsCateReportVo);
    });
    goodsCategoryList.forEach(goodsCategory -> {
      if (inventoryReportVoMap.get(goodsCategory.getId()) != null) {
        GoodsCateReportVo goodsCateReportVo = goodsCateReportVoMap.get(goodsCategory.getId());
        InventoryReportVo inventoryReportVo = inventoryReportVoMap.get(goodsCategory.getId());
        goodsCateReportVo.setGoodsCategoryId(goodsCategory.getId());
        goodsCateReportVo.setGoodsCategoryCode(goodsCategory.getCode());
        goodsCateReportVo.setGoodsCategoryName(goodsCategory.getName());
        goodsCateReportVo.setGoodsCateLevel(goodsCategory.getCateLevel());
        goodsCateReportVo.setPurQuantity(goodsCateReportVo.getPurQuantity().add(inventoryReportVo.getPurQuantity()));
        goodsCateReportVo.setPurAmt(goodsCateReportVo.getPurAmt().add(inventoryReportVo.getPurAmount()));
        goodsCateReportVo.setPurRetQuantity(goodsCateReportVo.getPurRetQuantity().add(inventoryReportVo.getPurRetQuantity()));
        goodsCateReportVo.setPurRetAmt(goodsCateReportVo.getPurRetAmt().add(inventoryReportVo.getPurRetAmount()));
        goodsCateReportVo.setLossQuantity(goodsCateReportVo.getLossQuantity().add(inventoryReportVo.getPurLossQuantity().add(inventoryReportVo.getPurRetLossQuantity())));
        goodsCateReportVo.setLossAmt(goodsCateReportVo.getLossAmt().add(inventoryReportVo.getPurLossAmount().add(inventoryReportVo.getPurRetLossAmount())));
        goodsCateReportVo.setSpillQuantity(goodsCateReportVo.getSpillQuantity().add(inventoryReportVo.getPurSpillQuantity().add(inventoryReportVo.getPurSpillQuantity())));
        goodsCateReportVo.setSpillAmt(goodsCateReportVo.getSpillAmt().add(inventoryReportVo.getPurSpillAmount().add(inventoryReportVo.getPurSpillAmount())));
        this.setSupGoodsCateReport(goodsCategory.getSupId(),goodsCateReportVoMap,inventoryReportVo);
      }
    });
    goodsCategoryList.forEach(goodsCategory -> {
      GoodsCateReportVo goodsCateReportVo = goodsCateReportVoMap.get(goodsCategory.getId());
      //纠正符号
      goodsCateReportVo.setPurRetQuantity(goodsCateReportVo.getPurRetQuantity().negate());
      goodsCateReportVo.setPurRetAmt(goodsCateReportVo.getPurRetAmt().negate());
      goodsCateReportVo.setLossQuantity(goodsCateReportVo.getLossQuantity().negate());
      goodsCateReportVo.setLossAmt(goodsCateReportVo.getLossAmt().negate());
      goodsCateReportVos.add(goodsCateReportVoMap.get(goodsCategory.getId()));
    });
    return goodsCateReportVos;
  }

  private void setSupGoodsCateReport(Long supId,Map<Long, GoodsCateReportVo> goodsCateReportVoMap,InventoryReportVo inventoryReportVo) {
    if (supId != null && goodsCateReportVoMap.get(supId) != null) {
      GoodsCateReportVo goodsCateReportVo = goodsCateReportVoMap.get(supId);
      goodsCateReportVo.setPurQuantity(goodsCateReportVo.getPurQuantity().add(inventoryReportVo.getPurQuantity()));
      goodsCateReportVo.setPurAmt(goodsCateReportVo.getPurAmt().add(inventoryReportVo.getPurAmount()));
      goodsCateReportVo.setPurRetQuantity(goodsCateReportVo.getPurRetQuantity().add(inventoryReportVo.getPurRetQuantity()));
      goodsCateReportVo.setPurRetAmt(goodsCateReportVo.getPurRetAmt().add(inventoryReportVo.getPurRetAmount()));
      goodsCateReportVo.setLossQuantity(goodsCateReportVo.getLossQuantity().add(inventoryReportVo.getPurLossQuantity().add(inventoryReportVo.getPurRetLossQuantity())));
      goodsCateReportVo.setLossAmt(goodsCateReportVo.getLossAmt().add(inventoryReportVo.getPurLossAmount().add(inventoryReportVo.getPurRetLossAmount())));
      goodsCateReportVo.setSpillQuantity(goodsCateReportVo.getSpillQuantity().add(inventoryReportVo.getPurSpillQuantity().add(inventoryReportVo.getPurSpillQuantity())));
      goodsCateReportVo.setSpillAmt(goodsCateReportVo.getSpillAmt().add(inventoryReportVo.getPurSpillAmount().add(inventoryReportVo.getPurSpillAmount())));
      this.setSupGoodsCateReport(goodsCateReportVo.getGoodsSupCategoryId(),goodsCateReportVoMap,inventoryReportVo);
    }
  }

  /**
   * 根据商品分类统计总条数
   *
   * @param params
   * @return
   */
  @Override
  public Integer goodsCateReportReportCount(Map<String, Object> params) {
//    return MybatisTemplate.selectOne("purchaseReportQRepository.goodsCateReportReportCount",params);
    GoodsCategoryReadRepository goodsCategoryReadRepository = (GoodsCategoryReadRepository) SpringContextUtils.getBean("goodsCategoryRepositoryImpl");
    Wrapper goodsCategoryWp = Wrapper.build();
    this.setQuery(goodsCategoryWp,params);
    return goodsCategoryReadRepository.getCount(goodsCategoryWp);
  }

  /**
   * 根据商品品牌统计采购
   *
   * @param params
   * @return
   */
  @Override
  public List<GoodsBrandReportVo> goodsBrandReport(Map<String, Object> params) {
//    return MybatisTemplate.selectList("purchaseReportQRepository.goodsBrandReport",params);
    return this.getGoodsBrandReport(params);
  }

  private List<GoodsBrandReportVo> getGoodsBrandReport(Map<String, Object> params) {
    GoodsBrandReadRepository goodsBrandReadRepository = (GoodsBrandReadRepository) SpringContextUtils.getBean("goodsBrandRepositoryImpl");
    //获取要分页统计的商品列表
    Integer startRow = (Integer) params.get("startRow");
    Integer pageSize = (Integer) params.get("pageSize");
    Long businessEntityId = (Long) params.get("businessEntityId");
    //报表统计开始时间 如果没有选择开始时间  默认是从最初开始查询
    String reportBeginTimeStamp = params.get("beginTime") == null ? "0" : (String)params.get("beginTime");
    //如果没有选择结束时间  默认到当前时间
    String reportEndTimeStamp = params.get("endTime") == null ? String.valueOf(System.currentTimeMillis()) : (String)params.get("endTime");
    Wrapper goodsCategoryWp = Wrapper.build();
    this.setQuery(goodsCategoryWp,params);
    List<GoodsBrand> goodsBrandList = goodsBrandReadRepository.findListPage(goodsCategoryWp,startRow,pageSize);
    Map<String,InventoryReportVo> inventoryReportVoMapTemp = StockInventoryUtils.getStockInventory(reportBeginTimeStamp,reportEndTimeStamp,businessEntityId);
    Map<Long,InventoryReportVo> inventoryReportVoMap = new HashMap<>();
    //把获取到的InventoryReportVo数据按商品品牌重新汇总成inventoryReportVoMap
    inventoryReportVoMapTemp.forEach((key,inventoryReportVo) -> {
      if (inventoryReportVoMap.get(inventoryReportVo.getGoodsBrandId()) != null) {
        InventoryReportVo inventoryReportVo1 = inventoryReportVoMap.get(inventoryReportVo.getGoodsBrandId());
        inventoryReportVo1.setPurQuantity(inventoryReportVo1.getPurQuantity().add(inventoryReportVo.getPurQuantity()));
        inventoryReportVo1.setPurAmount(inventoryReportVo1.getPurAmount().add(inventoryReportVo.getPurAmount()));
        inventoryReportVo1.setPurRetQuantity(inventoryReportVo1.getPurRetQuantity().add(inventoryReportVo.getPurRetQuantity()));
        inventoryReportVo1.setPurRetAmount(inventoryReportVo1.getPurRetAmount().add(inventoryReportVo.getPurRetAmount()));
        inventoryReportVo1.setPurLossQuantity(inventoryReportVo1.getPurLossQuantity().add(inventoryReportVo.getPurLossQuantity()));
        inventoryReportVo1.setPurLossAmount(inventoryReportVo1.getPurLossAmount().add(inventoryReportVo.getPurLossAmount()));
        inventoryReportVo1.setPurRetLossQuantity(inventoryReportVo1.getPurRetLossQuantity().add(inventoryReportVo.getPurRetLossQuantity()));
        inventoryReportVo1.setPurRetLossAmount(inventoryReportVo1.getPurRetLossAmount().add(inventoryReportVo.getPurRetLossAmount()));
        inventoryReportVo1.setPurSpillQuantity(inventoryReportVo1.getPurSpillQuantity().add(inventoryReportVo.getPurSpillQuantity()));
        inventoryReportVo1.setPurSpillAmount(inventoryReportVo1.getPurSpillAmount().add(inventoryReportVo.getPurSpillAmount()));
        inventoryReportVo1.setPurRetSpillQuantity(inventoryReportVo1.getPurRetSpillQuantity().add(inventoryReportVo.getPurRetSpillQuantity()));
        inventoryReportVo1.setPurRetSpillAmount(inventoryReportVo1.getPurRetSpillAmount().add(inventoryReportVo.getPurRetSpillAmount()));
      } else {
        inventoryReportVoMap.put(inventoryReportVo.getGoodsBrandId(),inventoryReportVo);
      }
    });
    List<GoodsBrandReportVo> goodsBrandReportVos = new ArrayList<>();
    goodsBrandList.forEach(goodsBrand -> {
      GoodsBrandReportVo goodsBrandReportVo = new GoodsBrandReportVo();
      goodsBrandReportVo.setGoodsBrandId(goodsBrand.getId());
      goodsBrandReportVo.setGoodsBrandName(goodsBrand.getName());
      goodsBrandReportVo.setPurQuantity(BigDecimal.ZERO);
      goodsBrandReportVo.setPurAmt(BigDecimal.ZERO);
      goodsBrandReportVo.setPurRetQuantity(BigDecimal.ZERO);
      goodsBrandReportVo.setPurRetAmt(BigDecimal.ZERO);
      goodsBrandReportVo.setLossQuantity(BigDecimal.ZERO);
      goodsBrandReportVo.setLossAmt(BigDecimal.ZERO);
      goodsBrandReportVo.setSpillQuantity(BigDecimal.ZERO);
      goodsBrandReportVo.setSpillAmt(BigDecimal.ZERO);

      //符号纠正
      goodsBrandReportVo.setPurRetQuantity(goodsBrandReportVo.getPurRetQuantity().negate());
      goodsBrandReportVo.setPurRetAmt(goodsBrandReportVo.getPurRetAmt().negate());
      goodsBrandReportVo.setLossQuantity(goodsBrandReportVo.getLossQuantity().negate());
      goodsBrandReportVo.setLossAmt(goodsBrandReportVo.getLossAmt().negate());
      goodsBrandReportVos.add(goodsBrandReportVo);
    });
    return goodsBrandReportVos;
  }

  /**
   * 根据商品品牌统计总条数
   *
   * @param params
   * @return
   */
  @Override
  public Integer goodsBrandReportCount(Map<String, Object> params) {
//    return MybatisTemplate.selectOne("purchaseReportQRepository.goodsBrandReportCount",params);
    GoodsBrandReadRepository goodsBrandReadRepository = (GoodsBrandReadRepository) SpringContextUtils.getBean("goodsBrandRepositoryImpl");
    Wrapper goodsCategoryWp = Wrapper.build();
    this.setQuery(goodsCategoryWp,params);
    return goodsBrandReadRepository.getCount(goodsCategoryWp);
  }
}
